package com.devbobcorn.oddities.common;

import java.util.Optional;

import com.devbobcorn.oddities.Oddities;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.event.TickEvent.Type;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@EventBusSubscriber(value = Dist.CLIENT, modid=Oddities.MODID, bus=Bus.FORGE)
public class ForgeClientEvents {
    public static int timeScale = 2;
    public static int tickCount = 0;

    @SubscribeEvent
    public static void onGameTick(TickEvent event) {
        if (event.type != Type.WORLD || event.phase != Phase.END || event.isCanceled()) {
            return;
        }

        tickCount++;

        if (tickCount % timeScale != 0) return;

        Optional<Integer> playSpeed = CloudSettings.getDebugParameterInt("play_speed");
        if (playSpeed.isPresent() && playSpeed.get() > 0) {
            Optional<Integer> playTick = CloudSettings.getDebugParameterInt("play_tick");

            if (!playTick.isPresent()) {
                CloudSettings.setDebugParameterInt("play_tick", 0);
            } else {
                // Increase tick
                int newTick = playTick.get() + playSpeed.get();
                Optional<Integer> maxTick = CloudSettings.getDebugParameterInt("max_tick");
                if (maxTick.isPresent() && maxTick.get() >= newTick) {
                    // Increase tick only when a max tick is present and not yet reached
                    CloudSettings.setDebugParameterInt("play_tick", newTick);
                }
            }

        }
    }
}
